import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app/utils/EventBus.dart';
import 'package:flutter_app/utils/http.dart';
import 'package:oktoast/oktoast.dart';

class CardDetail extends StatefulWidget {

  CardDetail({Key key, @required this.id}): super(key: key);
  final String id;

  @override
  _CardDetail createState() => _CardDetail();
}

class _CardDetail extends State<CardDetail> {

  @override
  void initState(){
    super.initState();
    bus.emit("push", "1");
    // 延迟操作
    Future.delayed(Duration.zero, (){
      // 暂时先传一个参数
      getDetail(widget.id);
    });
  }

  //  未处理：1
  var details;
  var dealType;
  var alarmType;
  var buttonHeight = 60.0;
  var alarmPicture = Container();
  var alarmTypeName = Container();
  var acquisitionTime = Container();
  var acquisitionPlace = Container();
  var deviceName = Container();
  var dealTypeName = Container();
  var remark = Container();
  var bus = new EventBus();
  var picHeight = 0.0;
  var picUrl = '';
  // 格式组件
  insideContainer(String title, String content, { borderShow = true }){
    var border = BoxDecoration();
    if(borderShow){
      border = BoxDecoration(
        border:Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)))
      );
    }
    return Container(
      padding: EdgeInsets.only(bottom: 8, top:8),
      decoration: border,
      child:Row(
        children: [
          Text("$title:", style: TextStyle(fontSize: 14, color: Color.fromRGBO(102, 102, 102, 0.8))),
          Text(content, style: TextStyle(fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)))
        ],
      ),
    );
  }

  GlobalKey _formKey = new GlobalKey<FormState>();
  TextEditingController _remarkController = new TextEditingController();

  submit() async{
    if ((_formKey.currentState as FormState).validate()) {
      ResultData res = await HttpRequest.post('/webauth/alarm/updateAlarm', {
        'id':details['id'],
        'version':details['version'],
        'remark':_remarkController.text,
      });
      print(res);
      if(res.code == 0){
        showToast(res.msg);
      }else{
        showToast("信息已处理");
      }
      Timer(Duration(seconds: 1), () {
        bus.emit("push", "1");
        Navigator.of(context).pop();
      });

    }
  }

  getDetail(id) async{
    ResultData card = await HttpRequest.post('/webauth/alarm/queryAlarmDetail', {
      'id':id,
    });
    details = card.data;
    if(details['alarmType'] == 'alarmType1'){
      alarmType = '陌生人告警';
    } else if(details['alarmType'] == 'alarmType2'){
      alarmType = '烟雾火警告警';
    } else if(details['alarmType'] == 'alarmType3'){
      alarmType = '访客异常告警';
    } else if(details['alarmType'] == 'alarmType4'){
      alarmType = '危险人物告警';
    } else if(details['alarmType'] == 'alarmType5'){
      alarmType = '陌生人告警';
    }
    picHeight = 150.0;
    picUrl = HttpRequest.BASE_IMG_URL + "/" + details['matchImage'];
    dealType = details['dealType'];
    if(dealType == "0"){
      buttonHeight = 0.0;
      setState(() =>{
        alarmTypeName = insideContainer('预警类型', alarmType),
        acquisitionTime = insideContainer('抓拍时间', details['acquisitionTime']),
        acquisitionPlace = insideContainer('抓拍地点', details['acquisitionPlace']),
        deviceName = insideContainer('设备名称', details['deviceName'], borderShow:false ),
        dealTypeName = insideContainer('处理状态', '已处理'),
        remark = insideContainer('备注', details['remark'])
      });
    } else {
      setState(() =>{
        alarmTypeName = insideContainer('预警类型', alarmType),
        acquisitionTime = insideContainer('抓拍时间', details['acquisitionTime']),
        acquisitionPlace = insideContainer('抓拍地点', details['acquisitionPlace']),
        deviceName = insideContainer('设备名称', details['deviceName'], borderShow:false ),
        dealTypeName = insideContainer('处理状态', '未处理'),
        remark = Container(
          child: Form(
            key: _formKey,
            child: TextFormField(
                autofocus: false,
                controller: _remarkController,
                decoration: InputDecoration(
                  labelText: "备注",
                  hintText: "请输入处理意见",
                  helperText:'点击输入建议',
                  // counterText: "0/100",
                  // maxLength:100,
                  border: InputBorder.none,
                ),
                maxLines: 4,
                maxLength:100,
//                validator: (v) {
//                  return v.trim().isNotEmpty ? null : '意见不能为空';
//                }
            ),
          ),
        )
      });
    }
    // print(card.data);

  }

  @override
  Widget build(BuildContext context) {

    // var args=ModalRoute.of(context).settings.arguments;
    return Scaffold(
      // 边距都是15
      backgroundColor: Color.fromRGBO(246, 247, 249, 1),
      // backgroundColor: Color(0xF6F7F9FF),
      appBar: AppBar(
        title: Text('告警处理'),
        centerTitle: true,
      ),
      body: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child:SafeArea(
            child:Column(
              children: [
                Container(
                    padding: EdgeInsets.only(left: 15, top: 15, bottom: 15),
                    child: Row(
                      children: [
                        Text("告警信息确认", style: TextStyle(fontSize: 12, color: Color.fromRGBO(102, 102, 102, 1)))
                      ],
                    )
                ),
                Container(
                    decoration: new BoxDecoration(color: Color.fromRGBO(255, 255, 255, 1)),
                    padding: EdgeInsets.only(left: 15, top: 7, bottom: 7, right: 15),
                    margin: EdgeInsets.only(bottom: 8),
                    child: Column(
                      children: [
                        alarmPicture,
                        alarmTypeName,
                        acquisitionTime,
                        acquisitionPlace,
                        deviceName,
                        Image.network(
                          picUrl,
                          fit: BoxFit.fill,
                          height: picHeight,
                          width: 450,
                        )
                  ],
                    )
                ),
                Container(
                    decoration: new BoxDecoration(color: Color.fromRGBO(255, 255, 255, 1)),
                    padding: EdgeInsets.only(left: 15, top: 7, bottom: 7, right: 15),
                    margin: EdgeInsets.only(bottom: 8),
                    child: Column(
                        children: [
                          dealTypeName,
                          remark,
                        ]
                    )
                )
              ],
            )
        )
      ),
      bottomNavigationBar:Container(
        padding: EdgeInsets.only(left: 15, right: 15),
        height: buttonHeight,
        decoration: new BoxDecoration(color: Color.fromRGBO(255, 255, 255, 1)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: double.infinity,
              child: (
                RaisedButton(
                  color: Colors.blueAccent,
                  child: Text("处理",style: TextStyle(fontSize: 16.0,color: Colors.white)),
                  // 设置按钮圆角
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
                  onPressed: submit
                )
              ),
            )
          ],
        )
      )
    );
  }
}